package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillFeeWriteOffItemBiz;
import com.egao.orderB.entity.BillFeeWriteOffItem;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-04-19 17:05:50
 * @version ：1.0
 */
@Repository
public class BillFeeWriteOffItemBizImpl extends BaseDao implements BillFeeWriteOffItemBiz {

	@Override
	public JsonResult list(PageResult2<T> page, BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeWriteOffItem> rm = new BeanPropertyRowMapper<BillFeeWriteOffItem>(BillFeeWriteOffItem.class);
		StringBuffer sql = new StringBuffer("select b.id,b.write_off_id,b.bill_id,b.invoicename,b.bill_of_lading");
        sql.append(",b.box_num,b.pack_date,b.settlement_price");
		sql.append(",b.type,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off_item b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" where b.deleted=0 and write_off_id=:write_off_id ");
		map.put("write_off_id", billFeeWriteOffItem.getWriteOffId());
    	if(Utils.isNotEmpty(billFeeWriteOffItem)){
//			if(Utils.isNotEmpty(billFeeWriteOffItem.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billFeeWriteOffItem.getName());
//			}
			if(Utils.isNotEmpty(billFeeWriteOffItem.getSort())&&Utils.isNotEmpty(billFeeWriteOffItem.getOrder())){
                sql.append(" order by b."+billFeeWriteOffItem.getSort()+" "+billFeeWriteOffItem.getOrder());
            } else {
                sql.append(" order by b.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BillFeeWriteOffItem> list(BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeWriteOffItem> rm = new BeanPropertyRowMapper<BillFeeWriteOffItem>(BillFeeWriteOffItem.class);
        StringBuffer sql = new StringBuffer("select b.id,b.write_off_id,b.bill_id,b.invoicename,b.bill_of_lading");
        sql.append(",b.box_num,b.pack_date,b.settlement_price");
		sql.append(",b.type,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off_item b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
		sql.append(" where b.deleted=0 and write_off_id=:write_off_id ");
		map.put("write_off_id", billFeeWriteOffItem.getWriteOffId());
    	if(Utils.isNotEmpty(billFeeWriteOffItem)){
//			if(Utils.isNotEmpty(billFeeWriteOffItem.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billFeeWriteOffItem.getName());
//			}
            if(Utils.isNotEmpty(billFeeWriteOffItem.getSort())&&Utils.isNotEmpty(billFeeWriteOffItem.getOrder())){
				sql.append(" order by b."+billFeeWriteOffItem.getSort()+" "+billFeeWriteOffItem.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public BillFeeWriteOffItem getById(BillFeeWriteOffItem billFeeWriteOffItem) {
		RowMapper<BillFeeWriteOffItem> rm = new BeanPropertyRowMapper<BillFeeWriteOffItem>(BillFeeWriteOffItem.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.write_off_id,b.bill_id,b.invoicename,b.bill_of_lading");
        sql.append(",b.box_num,b.pack_date,b.settlement_price");
		sql.append(",b.type,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off_item b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" where b.deleted=0 and  b.id=:id ");
		map.put("id", billFeeWriteOffItem.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_fee_write_off_item (");
        sql.append("write_off_id,bill_id,invoicename,bill_of_lading,box_num,pack_date");
		sql.append(",settlement_price,type,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":write_off_id,:bill_id,:invoicename,:bill_of_lading,:box_num,:pack_date");
		sql.append(",:settlement_price,:type,:remark");
		sql.append(",:create_user,now())");
        map.put("write_off_id", billFeeWriteOffItem.getWriteOffId());
        map.put("bill_id", billFeeWriteOffItem.getBillId());
        map.put("invoicename", billFeeWriteOffItem.getInvoicename());
        map.put("bill_of_lading", billFeeWriteOffItem.getBillOfLading());
        map.put("box_num", billFeeWriteOffItem.getBoxNum());
        map.put("pack_date", billFeeWriteOffItem.getPackDate());
        map.put("settlement_price", billFeeWriteOffItem.getSettlementPrice());
        map.put("type", billFeeWriteOffItem.getType());
        map.put("remark", billFeeWriteOffItem.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_bill_fee_write_off_item set ");
        sql.append("bill_id=:bill_id,invoicename=:invoicename,bill_of_lading=:bill_of_lading,box_num=:box_num ");
		sql.append(",pack_date=:pack_date,settlement_price=:settlement_price ");
		sql.append(",type=:type,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("bill_id", billFeeWriteOffItem.getBillId());
        map.put("invoicename", billFeeWriteOffItem.getInvoicename());
        map.put("bill_of_lading", billFeeWriteOffItem.getBillOfLading());
        map.put("box_num", billFeeWriteOffItem.getBoxNum());
        map.put("pack_date", billFeeWriteOffItem.getPackDate());
        map.put("settlement_price", billFeeWriteOffItem.getSettlementPrice());
        map.put("type", billFeeWriteOffItem.getType());
        map.put("remark", billFeeWriteOffItem.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", billFeeWriteOffItem.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(BillFeeWriteOffItem billFeeWriteOffItem, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(billFeeWriteOffItem.getId().length()>0){
			for(String ID : billFeeWriteOffItem.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_bill_fee_write_off_item set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
